Socket
Socket
Sign inDemoInstall

moment-range

Package Overview
Dependencies
Maintainers
5
Versions
35
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

moment-range

Fancy date ranges for Moment.js


Version published
Weekly downloads
371K
increased by6.5%
Maintainers
5
Weekly downloads
 
Created

What is moment-range?

The moment-range npm package extends the functionality of the Moment.js library to handle date ranges. It allows you to create, manipulate, and query ranges of dates with ease.

What are moment-range's main functionalities?

Creating Date Ranges

This feature allows you to create a date range by specifying a start and end date. The code sample demonstrates how to create a date range from January 1, 2023, to December 31, 2023.

const moment = require('moment');
const { extendMoment } = require('moment-range');
const momentRange = extendMoment(moment);

const start = moment('2023-01-01');
const end = moment('2023-12-31');
const range = momentRange.range(start, end);
console.log(range.toString());

Checking if a Date is within a Range

This feature allows you to check if a specific date falls within a given date range. The code sample checks if June 15, 2023, is within the range from January 1, 2023, to December 31, 2023.

const moment = require('moment');
const { extendMoment } = require('moment-range');
const momentRange = extendMoment(moment);

const start = moment('2023-01-01');
const end = moment('2023-12-31');
const range = momentRange.range(start, end);

const date = moment('2023-06-15');
console.log(range.contains(date)); // true

Iterating over Date Ranges

This feature allows you to iterate over each day within a date range. The code sample iterates over each day from January 1, 2023, to January 7, 2023, and prints the date.

const moment = require('moment');
const { extendMoment } = require('moment-range');
const momentRange = extendMoment(moment);

const start = moment('2023-01-01');
const end = moment('2023-01-07');
const range = momentRange.range(start, end);

for (let day of range.by('day')) {
  console.log(day.format('YYYY-MM-DD'));
}

Date Range Intersections

This feature allows you to find the intersection of two date ranges. The code sample finds the intersection of two date ranges: January 1, 2023, to June 30, 2023, and April 1, 2023, to December 31, 2023.

const moment = require('moment');
const { extendMoment } = require('moment-range');
const momentRange = extendMoment(moment);

const range1 = momentRange.range('2023-01-01', '2023-06-30');
const range2 = momentRange.range('2023-04-01', '2023-12-31');
const intersection = range1.intersect(range2);
console.log(intersection.toString()); // 2023-04-01 - 2023-06-30

Other packages similar to moment-range

FAQs

Package last updated on 25 Feb 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc